package com.mycat.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Scanner;
import java.util.concurrent.Future;

/**
 * ${DESCRIPTION}
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-17
 */
public class AIOClient {

    AsynchronousSocketChannel client;
    InetSocketAddress serverAddress = new InetSocketAddress("localhost",8888);
    ByteBuffer sendBuff = ByteBuffer.allocate(1024);

    public AIOClient() throws IOException {
        client = AsynchronousSocketChannel.open();
        Future<?> f = client.connect(serverAddress);
        System.out.println("客户端已启动");
    }

    public void send(String content){
        sendBuff.clear();
        sendBuff.put(content.getBytes());
        sendBuff.flip();
        client.write(sendBuff);
    }

    public static void main(String[] args) throws IOException {
        AIOClient client = new AIOClient();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String content = scanner.nextLine();
            client.send(content);
        }
    }
}
